package HashSet_1;

import java.util.*;

public class HashSetStudy {
    public static void main(String[] args) {
        HashSet<Student> students = new HashSet<>();
        students.add(new Student("aaa", 18));
        students.add(new Student("bbb", 17));
        students.add(new Student("ccc", 15));
        System.out.println("----------使用ForEach遍历hashSet----------");
        //1.for
        for (Student ele : students) {
            System.out.println("ele = " + ele);
//            System.out.println("ele.getName() = " + ele.getName());
//            System.out.println("ele.getAge() = " + ele.getAge());
        }
        System.out.println("----------使用iterator遍历hashSet----------");
        Iterator<Student> iterator1 = students.iterator();
        while (iterator1.hasNext()) {
            System.out.println("iterator1.next() = " + iterator1.next());
        }
        /**
         * Map  键值对
         *   keySet()    获取所有键(key)的集合
         *   entrySet()
         *         返回set集合  元素的类型是啥？？ Map.Entry
         *           Map.Entry: 泛型接口  Entry<K,V>
         *                getKey()  getValue()
         *
         *  keySet()  entrySet()  Map.Entry：类型   Entry<k,V> getKey() getValue();
         */

        System.out.println("------使用迭代器遍历HashMap---------");
        HashMap<String, Student> stringStudentHashMap = new HashMap<>();
        stringStudentHashMap.put("aaa", new Student("aa", 28));
        stringStudentHashMap.put("bbb", new Student("bb", 18));
        stringStudentHashMap.put("ccc", new Student("c", 38));

        Set<Map.Entry<String, Student>> entries = stringStudentHashMap.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();

        while (iterator.hasNext()) {
            Map.Entry<String, Student> next = iterator.next();
            System.out.println("hashMap集合元素的键是:" + next.getKey() + ",值是:" + next.getValue());
        }


        //------------
        System.out.println("------高级forEach遍历结果------------");
        //键的集合
        Set<String> hashMap = stringStudentHashMap.keySet();
        //遍历键的集合  get(s)
        for (String s : hashMap) {
            System.out.println("stringStudentHashMap.get(s) = " + stringStudentHashMap.get(s));
        }

        System.out.println("----------另一个中遍历方式--------------");
        for (Map.Entry<String, Student> entry : stringStudentHashMap.entrySet()) {
            System.out.println(entry.getKey() + "------" + entry.getValue());
        }

    }
}